<?php

namespace App\Http\Requests;

use App\Model\Operators;
use App\Model\Partner;
use Illuminate\Foundation\Http\FormRequest;

class OperatorsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @param Operators $operators
     * @param Partner $partner
     * @return bool
     */
    public function authorize(Operators $operators, Partner $partner)
    {
        //判断当前用户是否有权限管理合伙人
//        $user = $this->user();
//        if ($user->type != 1) {
//            return false;
//        }
//        //判断修改对象所有者是否是当前用户或者上级
//        if ($this->isMethod('put')) {
//            $id = reset($this->route()->parameters);
//            $o = $operators->find($id);
//            $p = $partner->find($o->place_partner_id);
//            if ($user->id != $p->user_id) {
//                return false;
//            }
//        }
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|string',
            'name' => 'required|string',
            'phone' => [
                'required',
                'regex:/^1[345789][0-9]{9}$/',
                'unique:operators,phone,'.reset($this->route()->parameters)
            ],
            'id_card' => [
                'required',
                'regex:/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/'
            ],
            'bank_card_name' => 'required|string',
//            'bank_card_no' => [
//                'required',
//                'regex:/^([1-9]{1})(\d{14}|\d{18})$/'
//            ],
            'bank_name' => 'required|string',
            'agency_fee' => 'required|numeric'
        ];
    }

    public function messages()
    {
        return [
            'required' => ':attribute是必填',
            'phone.regex' => '手机号不正确',
//            'bank_card_no.regex' => '银行卡号不正确',
            'id_card.regex' => '身份证不正确',
            'phone.unique' => '手机号被使用',
            'agency_fee.numeric' => '金额错误'
        ];
    }

    public function attributes()
    {
        return [
            'name' => '负责人姓名',
            'phone' => '负责人手机',
            'bank_card_name' => '银行卡账户名',
            'bank_card_no' => '银行卡号',
            'bank_name' => '开户行',
            'title' => '运营商名称',
            'id_card' => '身份证',
            'agency_fee' => '金额'
        ];
    }
}
